package com.caissa.springboot.starter.leaf.autoconfigure;

import cn.hutool.core.util.StrUtil;
import com.base.springboot.starter.leaf.config.LeafProperties;
import com.base.springboot.starter.leaf.domain.EcpCommonException;
import com.base.springboot.starter.leaf.service.LeafService;
import com.base.springboot.starter.leaf.service.impl.LeafServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author：zhangqiang
 * @Date 2020/8/20 17:39
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(LeafProperties.class)
public class LeafAutoConfiguration {

    @Autowired
    private LeafProperties properties;

    @SneakyThrows
    @Bean
    public LeafService leafService() {
        if (StrUtil.isBlank(properties.getPrefix())) {
            log.error("LeafProperties: {}", properties.toString());
            throw new EcpCommonException("leafProperties prefix is null.");
        }
        log.info("LeafProperties: {}", properties.toString());
        return new LeafServiceImpl();
    }
}
